package com.groupmobile.quotes.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseQuote extends SQLiteOpenHelper {
	private boolean oncreate = false;
	
	private static final String NAME_DB = "QUOTE.db";
	private static final int VERSION = 1;
	private SQLiteDatabase mSQLiteDatabase = null;
	
	//TABLE QUOTE
	public final String QUOTE_TABLE = "QUOTE";
	public final String QUOTE_id = "id";
	public final String QUOTE_author = "author";
	public final String QUOTE_authorId = "authorId";
	public final String QUOTE_authorImage = "authorImage";
	public final String QUOTE_body = "body";
	public final String QUOTE_category = "category";
	public final String QUOTE_count = "count";
	public final String QUOTE_countValue = "count";
	
	//TABLE AUTHOR
	public final String AUTHOR_TABLE = "AUTHOR";
	public final String AUTHOR_id = "id";
	public final String AUTHOR_name = "name";
	public final String AUTHOR_name_image = "name_image";

	//---------------------------------------------------------------------------
	/**
	 * Khi muốn thao tác với csdl thì bạn cần phải gọi phương thức openDatabase()
	 * Khi không còn sử dụng bạn gọi colseDatabase() để đóng lại csdl
	 * @param context
	 */
	public DatabaseQuote(Context context) {
		super(context, NAME_DB, null, VERSION);
		System.out.println("DatabaseQuote db");
	}
	//---------------------------------------------------------------------------
	@Override
	public void onCreate(SQLiteDatabase db) {
		this.oncreate = true;
		//Create TABLE Quote
		db.execSQL("CREATE TABLE " + QUOTE_TABLE + " " +
				"(" +
				"id INTEGER PRIMARY KEY," +
				"author TEXT," +
				"authorId TEXT NOT NULL," +
				"authorImage TEXT," +
				"body TEXT," +
				"category TEXT," +
				"count TEXT," +
				"countValue INTEGER" +
				");");
		
		System.out.println("Tạo bảng Quote thành công");
		
		
		//Create TABLE Author
		db.execSQL("CREATE TABLE " + AUTHOR_TABLE + " " +
				"(" +
				"id INTEGER PRIMARY KEY, " +
				"name TEXT, " +
				"name_image TEXT);");
		System.out.println("Tạo bảng Author thành công");
	}
	//--------------------------------------------------------------------------
	/**
	 * Nếu CSDL đã được thêm thì isOnCreate = false
	 * ngược lại isOnCreate = true
	 * @return
	 */
	public boolean isOnCreate(){
		return this.oncreate;
	}
	//---------------------------------------------------------------------------
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS Quote");
		onCreate(db);

		System.out.println("onUpgrade db");
	}
	//---------------------------------------------------------------------------
	/**
	 * Mở Database. Nếu csdl chưa có sẽ được tự tạo ra
	 */
	public void openDatabase(){
		mSQLiteDatabase = this.getWritableDatabase();
	}
	//---------------------------------------------------------------------------
	/**
	 * Đóng Database
	 */
	public void closeDatabase(){
		this.close();
	}
	//---------------------------------------------------------------------------
	/**
	 * Thực hiện 1 câu lệnh sql
	 * @param sql
	 */
	public void execSQL(String sql){
		this.execSQL(sql);
	}
	//---------------------------------------------------------------------------
	/**
	 * Thực hiện 1 câu lệnh query
	 * @param query
	 * @return Cursor
	 */
	public Cursor getCursorQuery(String table, String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy){ 		
		return mSQLiteDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
	}
	//--------------------------------------------------THAO TÁC VỚI BẢNG QUOTE--------------------------
	/**
	 * Thêm 1 Quote
	 * @param quote
	 */
	public void insertQuote(Quote quote){
		ContentValues CV = new ContentValues();
		
		CV.put("id", quote.getId());
		CV.put("author", quote.getAuthor());
		CV.put("authorId", quote.getAuthorId());
		CV.put("authorImage", quote.getAuthorImage());
		CV.put("category", quote.getCategory());
		CV.put("count", quote.getCount());
		CV.put("countValue", quote.getCountValue());
		
		if(mSQLiteDatabase.isOpen()){
			mSQLiteDatabase.insert(QUOTE_TABLE, null, CV);
		}
	}
	
	/**
	 * Update 1 quote với id
	 * @param quote
	 */
	public void updateQuote(Quote quote){
		ContentValues CV = new ContentValues();
		
		CV.put("author", quote.getAuthor());
		CV.put("authorId", quote.getAuthorId());
		CV.put("authorImage", quote.getAuthorImage());
		CV.put("category", quote.getCategory());
		CV.put("count", quote.getCount());
		CV.put("countValue", quote.getCountValue());
		
		if(mSQLiteDatabase.isOpen()){
			mSQLiteDatabase.update(QUOTE_TABLE, CV, "id = " + quote.getId(), null);
		}
	}
	
	/**
	 * Delete 1 quote với id
	 * @param id
	 */
	public void deleteQuote(String id){
		if(mSQLiteDatabase.isOpen()){
			mSQLiteDatabase.delete(QUOTE_TABLE, "id = " + id, null);
		}
	}
	//--------------------------------------------------KẾT THÚC QUOTE---------------------------------
	
	//--------------------------------------------------THAO TÁC VỚI BẢNG AUTHOR--------------------------
	/**
	 * Thêm 1 Author
	 * @param Author
	 */
	public void insertAuthor(Author author){
		ContentValues CV = new ContentValues();
		
		CV.put(AUTHOR_id, author.getAuthor_id());
		CV.put(AUTHOR_name, author.getAuthor_name());
		CV.put(AUTHOR_name_image, author.getAuthor_name_image());
		
		if(mSQLiteDatabase.isOpen()){
			mSQLiteDatabase.insert(AUTHOR_TABLE, null, CV);
		}
	}
	
	/**
	 * Update 1 Author với id
	 * @param Author
	 */
	public void updateAuthor(Author author){
		ContentValues CV = new ContentValues();

		CV.put(AUTHOR_name, author.getAuthor_name());
		CV.put(AUTHOR_name_image, author.getAuthor_name_image());
		
		if(mSQLiteDatabase.isOpen()){
			mSQLiteDatabase.update(AUTHOR_TABLE, CV, AUTHOR_id + " = " + author.getAuthor_id(), null);
		}
	}
	
	/**
	 * Delete 1 Author với id
	 * @param id
	 */
	public void deleteAuthor(String id){
		if(mSQLiteDatabase.isOpen()){
			mSQLiteDatabase.delete(AUTHOR_TABLE, AUTHOR_id + " = " + id, null);
		}
	}
	//--------------------------------------------------KẾT THÚC AUTHOR---------------------------------
}
